package com.tai.cobrapp.activity;

import com.tai.cobrapp.R;
import com.tai.cobrapp.adapter.SurveyListAdapter;
import com.tai.cobrapp.backend.Backend;
import com.tai.cobrapp.model.Relevamiento;
import com.tai.cobrapp.model.Usuario;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ListSurveysActivity extends ListActivity{

	SurveyListAdapter adapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_surveys);

		adapter = new SurveyListAdapter(this, Backend.getInstance().ListSurveys(Usuario.getUserInstance().getId(), this));
		this.getListView().setAdapter(adapter);
		
		this.getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// aca obtener el id del elemento y luego ir a editarlo por ejemplo, o mostrar detalles
				// pasandole el ID como extras a otra actividad
				
				Intent i = new Intent(ListSurveysActivity.this, DetailSurveyActivity.class);
				// Le paso el objeto completo DEL TIPO SERIALIZABLE
				i.putExtra("idRelevamiento", ((Relevamiento)adapter.getItem(position)).getId());
				startActivity(i);
			}
		
		});
	}
	@Override
	public void onBackPressed() {
		Intent i = new Intent(ListSurveysActivity.this, OptionsMenuActivity.class);
		startActivity(i);
		super.onBackPressed();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	protected void onStop() {
		finish();
		super.onStop();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.main_menu:
			Intent i = new Intent(this, OptionsMenuActivity.class);
			startActivity(i);
			break;
		case R.id.log_out:
			Intent i2 = new Intent(this, LoginActivity.class);
			startActivity(i2);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
